//
//  NewsNextVIew.m
//  kr_36
//
//  Created by lmt on 16/1/25.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NewsNextVIew.h"
#import "LMTAFNetwork.h"
#import "NewsModel.h"
#import <UIImageView+WebCache.h>
#import "NewsDetailTableViewCell.h"
#import "NewsDetailViewController.h"

@interface NewsNextVIew ()<UITableViewDataSource, UITableViewDelegate, UIWebViewDelegate>

@property (nonatomic, retain) NSMutableArray *dataArr;/**< 作品信息 */

@property (nonatomic, retain) UIWebView *web;
@property (nonatomic, retain) UIView *head;
@property (nonatomic, retain) UILabel *titleLabel;/**< 标题 */

@property (nonatomic, retain) UIImageView *tou;/**< 作者头像 */
@property (nonatomic, retain) UILabel *zuo;/**< 作者名字 */
@property (nonatomic, retain) UILabel *summary;/**< 作者信息 */

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, assign) BOOL isH;/**< 记录隐藏 */
@property (nonatomic, assign) BOOL isN;/**< 记录夜间模式 */

@property (nonatomic, retain) UIVisualEffectView *effectView;/**< 毛玻璃视图 */
@property (nonatomic, retain) UIView *sliderView;/**< 改变视图 */
@property (nonatomic, retain) UISlider *slider;/**< 滑动控制器 */

@end

@implementation NewsNextVIew

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 103)];
    _web.delegate = self;
    
    [self.view addSubview:_web];
    _web.userInteractionEnabled = YES;
    
    // 默认夜间模式
    _isN = NO;
    
    // webView 头
    _web.scrollView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    
    self.head = [[UIView alloc] initWithFrame:CGRectMake(0, -200, self.view.frame.size.width, 200)];
    _head.backgroundColor = [UIColor whiteColor];
    [_web.scrollView addSubview:_head];
    
    
    // 标题
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 100)];
    _titleLabel.numberOfLines = 0;
    _titleLabel.font = [UIFont systemFontOfSize:20];
    [self.head addSubview:_titleLabel];
    
    // 作者信息
    UIView *zuozhe = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    [self.head addSubview:zuozhe];
    zuozhe.userInteractionEnabled = YES;
    
    self.tou = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 40, 40)];
    self.tou.layer.cornerRadius = 20;
    _tou.clipsToBounds = YES;
    [zuozhe addSubview:_tou];
    
    self.zuo = [[UILabel alloc] initWithFrame:CGRectMake(70, 20, 100, 40)];
    [zuozhe addSubview:self.zuo];
    
    self.summary = [[UILabel alloc] initWithFrame:CGRectMake(70, 60, 250, 40)];
    [zuozhe addSubview:_summary];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(self.view.frame.size.width - 60, 25, 50, 50);
    [button setImage:[UIImage imageNamed:@"xia"] forState:(UIControlStateNormal)];
    [zuozhe addSubview:button];
    
    [button addTarget:self action:@selector(hidden:) forControlEvents:(UIControlEventTouchUpInside)];
    
    _isH = NO;
    
    // 创建按钮菜单 tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 180, self.view.frame.size.width, 300) style:(UITableViewStylePlain)];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.hidden = YES;
    
    [_tableView registerClass:[NewsDetailTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    // 数据解析 内容
    [self dataHander];
    
    // 数据解析 作者
    [self dataZuozhe];
    
    // 创建按钮
    
    // 夜间模式
    UIButton *night_btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [night_btn setImage:[UIImage imageNamed:@"night_off"] forState:(UIControlStateNormal)];
    [night_btn addTarget:self action:@selector(night:) forControlEvents:(UIControlEventTouchUpInside)];
    night_btn.frame = CGRectMake(0, 0, 19, 19);
    UIBarButtonItem *nightBar = [[UIBarButtonItem alloc] initWithCustomView:night_btn];
    
    // 字体大小
    UIButton *font_btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [font_btn setImage:[UIImage imageNamed:@"font"] forState:(UIControlStateNormal)];
    [font_btn addTarget:self action:@selector(changeFont:) forControlEvents:(UIControlEventTouchUpInside)];
    font_btn.frame = CGRectMake(0, 0, 20, 20);
    UIBarButtonItem *fontBar = [[UIBarButtonItem alloc] initWithCustomView:font_btn];
    // 分享
    UIButton *share_btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [share_btn setImage:[UIImage imageNamed:@"share"] forState:(UIControlStateNormal)];
    [share_btn addTarget:self action:@selector(share:) forControlEvents:(UIControlEventTouchUpInside)];
    share_btn.frame = CGRectMake(0, 0, 20, 20);
    UIBarButtonItem *shareBar = [[UIBarButtonItem alloc] initWithCustomView:share_btn];
    
    [self.navigationItem setRightBarButtonItems:@[nightBar, shareBar, fontBar]];

}

#pragma mark - 分享
- (void)share:(UIButton *)btn
{
    
}

#pragma mark - 改变字体大小
- (void)changeFont:(UIButton *)btn
{
    
    
    //  创建需要的毛玻璃特效类型
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    //  毛玻璃view 视图
    self.effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    //添加到要有毛玻璃特效的控件中
    _effectView.frame = self.view.bounds;
    [self.view addSubview:_effectView];
    //设置模糊透明度
    _effectView.alpha = 0.8;
    
    // 创建控制视图
    self.sliderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
    _sliderView.center = self.effectView.center;
    _sliderView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_sliderView];
    
    // 创建slider
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
    [_sliderView addSubview:_slider];
    
    _slider.maximumValue = 150;
    _slider.minimumValue = 50;
    _slider.value = 100;
    
    // 创建取消按钮和确认按钮
    UIButton *buttonCanel = [UIButton buttonWithType:(UIButtonTypeCustom)];
    buttonCanel.frame = CGRectMake(0, 50, 100, 50);
    [_sliderView addSubview:buttonCanel];
    [buttonCanel setTitle:@"取消" forState:(UIControlStateNormal)];
    [buttonCanel setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    [buttonCanel addTarget:self action:@selector(canel:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIButton *buttonQue = [UIButton buttonWithType:(UIButtonTypeCustom)];
    buttonQue.frame = CGRectMake(100, 50, 100, 50);
    [_sliderView addSubview:buttonQue];
    [buttonQue setTitle:@"确认" forState:(UIControlStateNormal)];
    [buttonQue setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    [buttonQue addTarget:self action:@selector(change:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    //字体大小
    //[_web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '120%'"];
}

- (void)canel:(UIButton *)btn
{
    [_sliderView removeFromSuperview];
    [_effectView removeFromSuperview];
}

- (void)change:(UIButton *)btn
{
    CGFloat m = _slider.value;
    NSString *str = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%lf%%'", m];
    //字体大小
    [_web stringByEvaluatingJavaScriptFromString:str];
    
    [_sliderView removeFromSuperview];
    [_effectView removeFromSuperview];
}


#pragma mark - 夜间模式
- (void)night:(UIButton *)btn
{
    _isN = !_isN;
    if (_isN) {
        
        [btn setImage:[UIImage imageNamed:@"night_on"] forState:(UIControlStateNormal)];
        [_web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='gray'"];
        
    } else {
        
        [btn setImage:[UIImage imageNamed:@"night_off"] forState:(UIControlStateNormal)];
        [_web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='white'"];
    }
    
}

- (void)hidden:(UIButton *)btn
{
    _isH = !_isH;
    
    if (_isH) {
        [btn setImage:[UIImage imageNamed:@"shang"] forState:(UIControlStateNormal)];
        _tableView.hidden = NO;
        [_tableView reloadData];
        
    } else {
        [btn setImage:[UIImage imageNamed:@"xia"] forState:(UIControlStateNormal)];
        _tableView.hidden = YES;
        
    }
    
    
}

- (void)dataZuozhe
{
    NSString *str = [NSString stringWithFormat:@"https://rong.36kr.com/api/mobi/news/%@/author-region", self.str];
    [LMTAFNetwork getUrl:str body:nil response:LMTJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        
        NSString *summary = resposeObject[@"data"][@"brief"];
        _summary.text = summary;
        
        NSArray *arr = resposeObject[@"data"][@"latestArticle"];
        self.dataArr = [NewsModel modelHanderWithArray:arr];
        
        [_tableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (void)dataHander
{
    NSString *str = [NSString stringWithFormat:@"https://rong.36kr.com/api/mobi/news/%@", self.str];
    
    [LMTAFNetwork getUrl:str body:nil response:LMTJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        
        NSLog(@"%@",self.str);
        
        NSDictionary *dic = resposeObject[@"data"];
        
        NSString *str = dic[@"content"];
        
        // 标题
        NSString *titleStr = dic[@"title"];
        _titleLabel.text = titleStr;
        
        // 作者头
        NSString *tou = dic[@"user"][@"avatar"];
        [_tou sd_setImageWithURL:[NSURL URLWithString:tou]];
        
        NSString *name = dic[@"user"][@"name"];
        _zuo.text = name;
        
        
        
        
        NSString *stHtml = [NSString stringWithFormat:@"<head><style>img{max-width:360px !important;}</style></head>%@", str];
        
        
        [_web loadHTMLString:stHtml baseURL:nil];
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    NewsModel *m = _dataArr[indexPath.row];
    [cell.tu sd_setImageWithURL:[NSURL URLWithString:m.featureImg]];
    cell.title.text = m.summary;
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NewsModel *m = _dataArr[indexPath.row];
    
    NewsDetailViewController *nnVC = [[NewsDetailViewController alloc] init];
    nnVC.str = m.postId;
    
    [self.navigationController pushViewController:nnVC animated:YES];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
